package com.matriq.cloudmanager.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public abstract class NetWorkRecevier extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		NetworkInfo.State wifiState = null;
		NetworkInfo.State mobileState = null;
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
		mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
				&& NetworkInfo.State.CONNECTED == mobileState) {
			// 手机网络连接成功
			onMobileConnected();
		} else if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
				&& NetworkInfo.State.CONNECTED != mobileState) {
			// 手机没有任何的网络
			onDisconnected();
		} else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
			// 无线网络连接成功
			onWifiConnected();
		} else {
			onDisconnected();
		}
	}
	
	public static IntentFilter getIntentFilter(){
		IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
		return filter;
	}

	public abstract void onWifiConnected();
	public abstract void onMobileConnected();
	public abstract void onDisconnected();
}
